package com.guguskins.back.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.common.constant.RedisConstant;
import com.guguskins.back.entity.GuSystemConfig;
import com.guguskins.back.mapper.GuSystemConfigMapper;
import com.guguskins.back.service.GuSystemConfigService;
import com.guguskins.back.utils.RedisUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-06-02
 */
@Service
public class GuSystemConfigServiceImpl extends ServiceImpl<GuSystemConfigMapper, GuSystemConfig> implements GuSystemConfigService {

    @Override
    public GuSystemConfig getSystemConfig() {
        GuSystemConfig systemConfig;
        if (!RedisUtil.hasKey(RedisConstant.SYSTEM_CONFIG)) {
            systemConfig = this.getById(1);
            RedisUtil.setKey(RedisConstant.SYSTEM_CONFIG, JSON.toJSONString(systemConfig));
        } else {
            String redisSystemConfigStr = RedisUtil.getValue(RedisConstant.SYSTEM_CONFIG);
            systemConfig = JSON.parseObject(redisSystemConfigStr, GuSystemConfig.class);
        }
        return systemConfig;
//        return this.getById(1);
    }

    @Override
    public boolean backEdit(GuSystemConfig systemConfig) {
        systemConfig.setConfigId(1);
        if (this.updateById(systemConfig)) {
            RedisUtil.deleteKey(RedisConstant.SYSTEM_CONFIG);
            return true;
        }
        return false;
    }
}
